package demo1.helloworld;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Producer {


    public static void main(String[] args) throws IOException, TimeoutException {
        /**
         * 创建连接连接到RabbitMQ
         */
        ConnectionFactory factory = new ConnectionFactory();
        // 设置RabbitMQ所在主机ip或者主机名
        factory.setHost("localhost");
        // 创建一个连接
        Connection connection = factory.newConnection();
        // 创建一个频道
        Channel channel = connection.createChannel();
        String message1 = "send to 123!";
        // 向对队列中发送一条信息
        String routingKey = "key.123.345";
        String exChangeName = "change1";
        channel.basicPublish(exChangeName, routingKey, null, message1.getBytes());
        System.out.println("[x] Producer to Recv1: " + message1);
        //关闭频道和连接
        channel.close();
        connection.close();
    }
}